package com.ruoyi.project.business.schedefine.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.schedefine.entity.ScheduleDefine;
import com.ruoyi.project.business.schedefine.request.ListAllScheduleDefineRequest;
import com.ruoyi.project.business.schedefine.response.ScheduleDefineResponse;
import com.ruoyi.project.business.schedefine.service.IScheduleDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班次定义Controller
 * 
 * @author ${author}
 * @date 2024-02-15
 */
@RestController
@RequestMapping("/business/schedefine")
public class ScheduleDefineController extends BaseController {
    @Autowired
    private IScheduleDefineService configScheDefineService;

    /**
     * 查询班次定义列表
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScheduleDefine configScheDefine) {
        startPage();
        Long tenantId = getTenantId();
        List<ScheduleDefine> list = configScheDefineService.selectConfigScheDefineList(configScheDefine, tenantId);
        return getDataTable(list);
    }

    /**
     * 查询班次定义列表
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:list')")
    @GetMapping("/list-all")
    public TableDataInfo listAll(ListAllScheduleDefineRequest request) {
        Long tenantId = getTenantId();
        List<ScheduleDefineResponse> list = configScheDefineService.selectAllOfDeptId(request, tenantId);
        return getDataTable(list);
    }

    /**
     * 导出班次定义列表
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:export')")
    @Log(title = "班次定义", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScheduleDefine configScheDefine) {
        Long tenantId = getTenantId();
        List<ScheduleDefine> list = configScheDefineService.selectConfigScheDefineList(configScheDefine, tenantId);
        ExcelUtil<ScheduleDefine> util = new ExcelUtil<ScheduleDefine>(ScheduleDefine.class);
        return util.exportExcel(list, "schedefine");
    }

    /**
     * 获取班次定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(configScheDefineService.selectConfigScheDefineById(id));
    }

    /**
     * 新增班次定义
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:add')")
    @Log(title = "班次定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScheduleDefine configScheDefine) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        configScheDefine.setScheduleType(1); // 类型暂时不知道怎么用，先全部给 1
        return toAjax(configScheDefineService.insertConfigScheDefine(configScheDefine, operatorId, tenantId));
    }

    /**
     * 修改班次定义
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:edit')")
    @Log(title = "班次定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScheduleDefine configScheDefine) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(configScheDefineService.updateConfigScheDefine(configScheDefine, operatorId, tenantId));
    }

    /**
     * 删除班次定义
     */
    @PreAuthorize("@ss.hasPermi('business:schedefine:remove')")
    @Log(title = "班次定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(configScheDefineService.deleteConfigScheDefineByIds(ids, operatorId, tenantId));
    }
}
